package device

import (
	"encoding/json"
	"gitee.com/zhanghuamingmyself/api"
	"gitee.com/zhanghuamingmyself/util"
)

var deviceBasePath = "iot/api/v1/v1/device/"

type IotDevice struct {
	api.BaseDTO
	DeviceCore  string  `json:"deviceCore"`
	Lat         float64 `json:"lat"`
	Lng         float64 `json:"lng"`
	Address     string  `json:"address"`
	Status      string  `json:"status"`
	PhoneStatus int32     `json:"phoneStatus"`
	PhoneNumber string  `json:"phoneNumber"`
	NetSignal   int32     `json:"netSignal"`
	Surplus     int32     `json:"surplus"`
	Charging    bool    `json:"charging"`
	Enable      bool    `json:"enable"`
	CanDeviceLinkage      bool    `json:"canDeviceLinkage"`
	LocationType      string    `json:"locationType"`
	ServerMessage      string    `json:"serverMessage"`
	WarnNum      int32    `json:"warnNum"`
	PingTime      int32    `json:"pingTime"`
}

type IotDeviceStatus struct {
	deviceCore string `json:"deviceCore"`
	status     string `json:"status"`
	timestamp  int32  `json:"timestamp"`
}

type IotDeviceListReq struct {
	Status         string   `json:"status"`
	DeviceCoreList []string `json:"deviceCoreList"`
}

func ListDevice(req IotDeviceListReq) ([]IotDevice, error) {
	var deviceList []IotDevice
	bytes, err := util.PostJson(deviceBasePath+"list", req)
	if err != nil {
		return deviceList, err
	}
	json.Unmarshal(bytes, &deviceList)
	return deviceList, nil
}

type DeviceGetReq struct {
	DeviceCore string `json:"deviceCore"`
}

func GetDevice(req DeviceGetReq) (IotDevice, error) {
	var device IotDevice
	bytes, err := util.PostJson(deviceBasePath+"get", req)
	if err != nil {
		return device, err
	}
	json.Unmarshal(bytes, &device)
	return device, nil
}

type IotDeviceBatchStatusReq struct {
	DeviceCoreList []string `json:"deviceCoreList"`
}

func BatchDeviceStatus(req IotDeviceBatchStatusReq) ([]IotDeviceStatus, error) {
	var deviceStatusList []IotDeviceStatus
	bytes, err := util.PostJson(deviceBasePath+"batchDeviceStatus", req)
	if err != nil {
		return deviceStatusList, err
	}
	json.Unmarshal(bytes, &deviceStatusList)
	return deviceStatusList, nil
}
